<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet"
	href="/static/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">
<link rel="stylesheet" href="/static/ztree/css/demo.css" type="text/css">
<script type="text/javascript" src="/static/jquery-3.2.1.js"></script>
<script type="text/javascript"
	src="/static/ztree/js/jquery.ztree.core.js"></script>
</head>


<body>
	<div class="content_wrap">
		<div class="zTreeDemoBackground left">
			<ul id="treeDemo" class="ztree"></ul>
		</div>
		<div class="right">
			tradeTypeCode:<input type="text" id="tradeTypeCode"><br>
			bossCode:<input type="text" id="bossCode">


			<hr />
			业务类型名称:<input type="text" id="tradeTypeName"><br> 是否99 :
			<input type="checkbox" name="is99"><br> <input
				type="button" value="新增" id="addTradeBtn">
			<hr />
			<!--
	bossCode:<input type="text" id="addBossCode" ><br>
	名称:<input type="text" id="addTradeTypeName" ><br>
	<input type="button" value="新增" id="newTradeBtn">
	-->
		</div>
	</div>


	<script type="text/javascript">
		var setting = {
			async : {
				enable : true,
				url : "/tradetype/getTreeNode",
				autoParam : [ "tradeTypeCode", "parentTypeCode", "id", "name" ],
				type : "get",
				dataType : "text",
				otherParam : {
					"test" : "test"
				},
				dataFilter : filter
			},
			data : {
				key : {
					name : "tradeTypeName",
					title : "tradeTypeName",
					bossCode : "bossCode",
					remark : "remark"
				},

				simpleData : {
					enable : true,
					idKey : "tradeTypeCode",
					pIdKey : "parentTypeCode",
					rootPId : "0"
				}
			},
			callback : {
				onClick : zTreeOnClick
			}
		};

		var currNodeId, bossCode, remark;
		function zTreeOnClick(event, treeId, treeNode) {
			//alert(treeNode.tId + ", " + treeNode.tradeTypeName+ "--"+treeNode.tradeTypeCode);
			currNodeId = treeNode.tradeTypeCode;
			bossCode = treeNode.bossCode;
			remark = treeNode.remark;
			$("#tradeTypeCode").val(treeNode.tradeTypeCode);
			$("#bossCode").val(treeNode.bossCode);
		};

		function filter(treeId, parentNode, childNodes) {
			if (!childNodes)
				return null;
			for (var i = 0, l = childNodes.length; i < l; i++) {
				childNodes[i].name = childNodes[i].tradeTypeName;
				childNodes[i].isParent = !childNodes[i].leaf;
			}
			return childNodes;
		}

		$(function() {

			$.fn.zTree.init($("#treeDemo"), setting);

			$("#addTradeBtn").bind("click", function() {
				var is99 = $("input[name='is99']:checked").val();

				if ($.trim($("#tradeTypeName").val()) == "") {
					alert("不能为空");
					return;
				}
				if (currNodeId == undefined) {
					alert("请选择父节点");
					return;
				}

				$.ajax({
					url : "/tradetype/add",
					type : "post",
					data : {
						currNodeId : currNodeId,
						name : $("#tradeTypeName").val(),
						remark : remark,
						bossCode : bossCode,
						is99 : is99

					},
					success : function(data) {
						//alert("新增成功");
						$("#tradeTypeName").val("");
						$("input[name='is99']").prop("checked", false);

						var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
						var nodes = treeObj.getSelectedNodes();
						if (nodes.length > 0) {
							treeObj.reAsyncChildNodes(nodes[0], "refresh");
						}
					},
					error : function(jqXHR, textStatus, errorThrown) {

					}
				});

			});

		});
	</script>
</body>

</html>